<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>订单页</title>
    <link rel="stylesheet" href="css/globe.css"/>
    <link rel="stylesheet" href="css/dish_list.css"/>
    <script type="text/javascript" src="js/jquery-1.11.0.min.js"></script>
    <script type="text/javascript" src="js/getParameter.js"></script>
    <script>
        $(function () {
            var userId = '<%=session.getAttribute("userId")%>';
            if (userId !== "null" && userId.length > 0) {
                localStorage.setItem("userId", userId);
            } else {
                alert("您还未登录，请登录");
                window.location.href = "${pageContext.request.contextPath}/login.jsp";
            }
            load(1, "<%=session.getAttribute("username")%>");
        });

        function dateFormat(str, format = 'YY-MM-DD hh:mm:ss') {
            var date = new Date(str);
            var year = date.getFullYear(),
                month = date.getMonth() + 1,
                day = date.getDate(),
                hour = date.getHours(),
                min = date.getMinutes(),
                sec = date.getSeconds();

            var preArr = Array.apply(null, Array(10)).map(function (elem, index) {
                return '0' + index;
            });

            var newTime = format.replace(/YY/g, year)
                .replace(/MM/g, preArr[month] || month)
                .replace(/DD/g, preArr[day] || day)
                .replace(/hh/g, preArr[hour] || hour)
                .replace(/mm/g, preArr[min] || min)
                .replace(/ss/g, preArr[sec] || sec);

            return newTime;
        }

        function load(currentPage, username) {
            $.ajax({
                url: "${pageContext.request.contextPath}/order/findOrderListByPage",
                type: "get",
                dataType: "json",
                data: {
                    currentPage: currentPage,
                    pageSize: 15,
                    username: username
                },
                success: function (data) {
                    if (data) {
                        $("#totalPage").html(data.totalPage);
                        $("#totalCount").html(data.totalCount);

                        var lis = "";
                        var firstPage = '<li onclick="javascript:load(1,\'' + username + '\')"><a href="javascript:void(0);">首页</a></li>';
                        var beforeNum = data.currentPage - 1;
                        if (beforeNum < 1) {
                            beforeNum = 1;
                        }
                        var beforePage = '<li onclick="javascript:load(' + beforeNum + ', \'' + username + '\')" class="threeword"><a href="javascript:void(0);">上一页</a></li>'
                        lis += firstPage;
                        lis += beforePage;

                        var begin;
                        var end;
                        if (data.totalPage < 10) {
                            begin = 1;
                            end = data.totalPage;
                        } else {
                            begin = data.currentPage - 5;
                            end = data.currentPage + 4;
                            if (begin < 1) {
                                begin = 1;
                                end = begin + 9;
                            }
                            if (end > data.totalPage) {
                                end = data.totalPage;
                                begin = end - 9;
                            }
                        }
                        for (var i = begin; i <= end; i++) {
                            var li;
                            if (data.currentPage === i) {
                                li = '<li class="curPage" onclick="javascript:load(' + i + ', \'' + username + '\');"><a href="javascript:void(0);">' + i + '</a></li>'
                            } else {
                                li = '<li onclick="javascript:load(' + i + ', \'' + username + '\');"><a href="javascript:void(0);">' + i + '</a></li>'
                            }
                            lis += li;
                        }

                        var preNum = data.currentPage + 1;
                        if (preNum > data.totalPage) {
                            preNum = data.totalPage;
                        }
                        var prePage = '<li onclick="javascript:load(' + preNum + ', \'' + username + '\');" class="threeword"><a href="javascript:;">下一页</a></li>'
                        var lastPage = '<li onclick="javascript:load(' + data.totalPage + ', \'' + username + '\');" class="threeword"><a href="javascript:;">末页</a></li>'

                        lis += prePage;
                        lis += lastPage;

                        $("#pageNum").html(lis);

                        var dish_lis = "";
                        for (var i = 0; i < data.list.length; i++) {
                            var order = data.list[i];
                            var li =
                                '<li>\n' +
                                '    <div class="text1">\n' +
                                '        <p>订单：' + order.orderId + '</p>\n' +
                                '        <br/>\n' +
                                '        <p>下单时间：' + dateFormat(order.createTime, 'YY年MM月DD日 hh:mm:ss') + '</p>\n' +
                                '        <p>收货人：' + order.user.trueName + '</p>\n' +
                                '        <p>手机号：' + order.user.telephone + '</p>\n' +
                                '        <p>收货地址：' + order.user.city + "&nbsp;" + order.user.address + '</p>\n' +
                                '        <p>订单详情：';
                            var orderDetailsList = order.orderDetailsList;
                            for (var j = 0; j < orderDetailsList.length; j++) {
                                if (j === 0) {
                                    li += orderDetailsList[j].dishName + '*' + orderDetailsList[j].quantity;
                                } else {
                                    li += '+' + orderDetailsList[j].dishName + '*' + orderDetailsList[j].quantity;
                                }
                            }
                            li +=
                                '        </p>\n' +
                                '    </div>\n' +
                                '    <div class="price">\n';
                            if (order.status === 0) {
                                li +=
                                    '<p>未发货</p>\n' +
                                    '<p><a href="#">查看详情</a></p>\n' +
                                    '<p><a href="#">取消交易</a></p>\n';
                            } else if (order.status === 1) {
                                li +=
                                    '<p>已发货</p>\n' +
                                    '<p><a href="#">查看详情</a></p>\n' +
                                    '<p><a href="#">确认收货</a></p>\n';
                            } else if (order.status === 2) {
                                li += '<p>交易完成</p>\n' +
                                    '<p><a href="#">查看详情</a></p>\n';
                            } else if (order.status === 3) {
                                li +=
                                    '<p>交易取消</p>\n' +
                                    '<p><a href="#">查看详情</a></p>\n';
                            }
                            li +=
                                '    </div>\n' +
                                '</li>';
                            dish_lis += li;
                        }
                        $("#dish").html(dish_lis);
                        window.scrollTo(0, 0);
                    }
                },
                error: function () {
                    alert("查询失败");
                }
            });
        }
    </script>
</head>
<body>
<jsp:include page="user_bar.jsp"/>
<div id="main_page">
    <div class="xinxi clearfix">
        <div class="dish_div">
            <div class="header">
                <span>订单信息</span>
            </div>
            <ul id="dish">
                <%--                <li>--%>
                <%--                    <div class="text1">--%>
                <%--                        <p>订单号：112213</p>--%>
                <%--                        <br/>--%>
                <%--                        <p>下单时间：</p>--%>
                <%--                        <p>收货人：</p>--%>
                <%--                        <p>手机号：</p>--%>
                <%--                        <p>收货地址：</p>--%>
                <%--                        <p>订单详情：</p>--%>
                <%--                    </div>--%>
                <%--                    <div class="price">--%>
                <%--                        <p>订单状态</p>--%>
                <%--                        <p><a href="#">查看详情</a></p>--%>
                <%--                        <p><a href="#">确认收货</a></p>--%>
                <%--                        <p><a href="#">删除订单</a></p>--%>
                <%--                    </div>--%>
                <%--                </li>--%>
            </ul>

            <div class="page_num_inf">
                <i></i>共
                <span id="totalPage"></span>页
                <span id="totalCount"></span>条
            </div>
            <div class="pageNum">
                <ul id="pageNum">
                    <%--                    <li><a href="">首页</a></li>--%>
                    <%--                    <li class="threeword"><a href="#">上一页</a></li>--%>
                    <%--                    <li><a href="#">1</a></li>--%>
                    <%--                    <li><a href="#">2</a></li>--%>
                    <%--                    <li><a href="#">3</a></li>--%>
                    <%--                    <li class="threeword"><a href="javascript:;">下一页</a></li>--%>
                    <%--                    <li class="threeword"><a href="javascript:;">末页</a></li>--%>
                </ul>
            </div>
        </div>


    </div>
</body>
</html>
